package com.zbg;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "ServletLogin",urlPatterns = "/ServletLogin")
public class ServletLogin extends HttpServlet {
    @Override
    public void destroy() {
        System.out.println("登录被销毁----------被销毁被创建");
    }

    @Override
    public void init() throws ServletException {
        System.out.println("登录被创建----------登录被创建");
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String userName = request.getParameter("userName");
        String pwd = request.getParameter("pwd");
        Cookie cookie = new Cookie("userName",userName);
        response.addCookie(cookie);
        Cookie cookie1 = new Cookie("pwd",pwd);
        response.addCookie(cookie1);
        request.getCookies();

        request.getSession().setAttribute("userName",userName);;
        System.out.println(request.getSession().getAttribute("userName"));
        System.out.println(getServletContext().getAttribute("a"));

        if ("123".equals(userName) && "456".equals(pwd)){
            response.sendRedirect("login/login.jsp");
        }else {
            response.sendRedirect("login/wel.html");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
